package com.liziyi0914.morearmor.effect;

import com.liziyi0914.morearmor.PlayerManager;
import com.liziyi0914.morearmor.Utils;
import com.liziyi0914.morearmor.event.Listener;
import com.liziyi0914.morearmor.event.entity.WorldTickEvent;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.damage.EntityDamageSource;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Items;
import net.minecraft.server.network.ServerPlayerEntity;

import java.util.function.Predicate;

/**
 * 2021/6/29<br>
 * MoreArmor<br>
 *
 * @author huanmeng_qwq
 */
public class MagmaBlockEffect extends AbstractEffect{
    @Listener
    public void onTick(WorldTickEvent e){
        for (Entity entity : e.getWorld().getEntities()) {
            if (entity instanceof PlayerEntity) {
                final ServerPlayerEntity player = PlayerManager.get(entity.getUuid());
                if (player != null ) {
                    if (predicateArmor().test(player)) {
                        for (LivingEntity livingEntity : getNearEntity(player.getServerWorld(), player, 4)) {
                            if(!livingEntity.isSneaking()){
                                livingEntity.damage(DamageSource.player(player),2.5f);
                            }
                        }
                    }
                }
            }
        }
    }

    @Override
    public Predicate<LivingEntity> predicateArmor() {
        return Utils.hasItemArmor(Items.MAGMA_BLOCK).or(Utils.hasItemArmor(Items.MAGMA_CREAM));
    }
}
